﻿using System.Linq;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Property
{
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Telerik.Windows.Controls;
    using System.Windows.Threading;
    using Telerik.Windows.Controls.Map;

    using System;
    using System.Net;

    /// <summary>
    /// <see cref="Page"/> class to present information about the current application.
    /// </summary>
    public partial class About : Page
    {
        DispatcherTimer zoomOutTimer = new DispatcherTimer();
        DispatcherTimer zoomInTimer = new DispatcherTimer();
        DispatcherTimer moveTimer = new DispatcherTimer();

        private bool providerChanged = false;
        private string VEKey = "An8TkWZIW5K69gB_dZ-T_PTG2wYzatup2hwaPSI1fKAhrp5eQDfjI0KhsQATE0ZI";
        private const int minZoomLevel = 3;
        private const int maxZoomLevel = 8;


        /// <summary>
        /// Creates a new instance of the <see cref="About"/> class.
        /// </summary>
        public About()
        {
            InitializeComponent();
            this.radMap1.Provider = new BingMapProvider(MapMode.Aerial, true, VEKey);

            // this.SetProvider();
            // LocationBox.SelectedIndex = -1;

            // zoomOutTimer.Interval = TimeSpan.FromMilliseconds(150);
            // zoomInTimer.Interval = TimeSpan.FromMilliseconds(150);
            // moveTimer.Interval = TimeSpan.FromMilliseconds(800);
            //zoomInTimer.Tick += new EventHandler(zoomInTimer_Tick);
            //zoomOutTimer.Tick += new EventHandler(zoomOutTimer_Tick);
            // moveTimer.Tick += new EventHandler(moveTimer_Tick);
            this.Title = ApplicationStrings.AboutPageTitle;
        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void RadTileView_TileStateChanged(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            RadTileViewItem item = e.Source as RadTileViewItem;
            if (item != null)
            {
                RadFluidContentControl fluidControl = item.Content as RadFluidContentControl;
                if (fluidControl != null)
                {
                    switch (item.TileState)
                    {
                        case TileViewItemState.Maximized:
                            fluidControl.State = FluidContentControlState.Large;
                            break;
                        case TileViewItemState.Minimized:
                            fluidControl.State = FluidContentControlState.Small;
                            break;
                        case TileViewItemState.Restored:
                            fluidControl.State = FluidContentControlState.Normal;
                            break;
                    }
                }
            }
        }

        //private void GetVEServiceKey()
        //{
        //    WebClient wc = new WebClient();
        //    wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
        //}

        //void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        //{
        //    this.SetProvider();
        //}

        void moveTimer_Tick(object sender, EventArgs e)
        {
           // this.moveTimer.Stop();
            //this.ZoomIn();
        }

        void zoomInTimer_Tick(object sender, EventArgs e)
        {
            if (this.radMap1.ZoomLevel < maxZoomLevel)
                this.radMap1.ZoomLevel++;
            else
                this.zoomInTimer.Stop();
        }

        void zoomOutTimer_Tick(object sender, EventArgs e)
        {
            if (this.radMap1.ZoomLevel > minZoomLevel)
                this.radMap1.ZoomLevel--;
            else
            {
                this.zoomOutTimer.Stop();
                //this.ZoomOutFinished();
            }
        }

        //void ZoomOutFinished()
        //{
        //    radMap1.Center = (Location)LocationBox.SelectedItem;
        //    this.SetProvider();
        //    this.moveTimer.Start();
        //}

        //private void SetProvider()
        //{
        //    string order = LocationBox.SelectedIndex.ToString();

        //    radMap1.Center = (Location)LocationBox.SelectedItem;

        //    var item = LocationBox.SelectedItem;

        //    string description = 

        //    //string description =  LocationBox.SelectedItem[0]

        //    if (order != null)
        //    {
        //        // Initialize RadMap
        //        RadMap1.Provider = new BingMapProvider(MapMode.Aerial, true, VEKey);
        //        RadMap1.ZoomLevel = 12;

        //        // Create the geocoding object
        //        BingGeocodeProvider geocodeProvider = new BingGeocodeProvider();
        //        geocodeProvider.ApplicationId = VEKey;
        //        geocodeProvider.MapControl = RadMap1;

        //        // Create a new address object to feed the geocoding service
        //        Address adr = new Address();
        //        adr.AddressLine = radMap1.Center.ToString();

        //        // Create the request object
        //        GeocodeRequest request = new GeocodeRequest { Address = adr };
        //        geocodeProvider.GeocodeCompleted += new EventHandler<GeocodeCompletedEventArgs>(geocodeProvider_GeocodeCompleted);
        //        // Request the address translation
        //        geocodeProvider.GeocodeAsync(request);
        //    }
        //}

        //private void geocodeProvider_GeocodeCompleted(object sender, GeocodeCompletedEventArgs e)
        //{
        //    // Grab the response from the geocoding service
        //    GeocodeResponse response = e.Response;
        //    this.informationlayer.Items.Clear();
        //    // Verify that there were results and take the
        //    // first one
        //    if (response.Results.Count == 0) return;
        //    var result = response.Results.First();
        //    // Now verify that there are actual locations (lat, long)
        //    // to work with and then take the first one
        //    if (result.Locations.Count == 0) return;
        //    Location l = result.Locations.First();
        //    // Create the pin point for the first location
        //    MapPinPoint pinPoint = new MapPinPoint()
        //    {
        //        Background = new SolidColorBrush(Colors.White),
        //        Foreground = new SolidColorBrush(Colors.Red),
        //        FontSize = 10,
        //        ImageSource = new BitmapImage(new Uri("Images/PinPoint.png", UriKind.Relative)),
        //        Text = result.Address.FormattedAddress
        //    };
        //    Location loc = new Location(l.Latitude, l.Longitude);
        //    RadMap1.Center = loc;
        //    MapLayer.SetLocation(pinPoint, loc);
        //    // Add the pin point to the information layer
        //    this.informationlayer.Items.Add(pinPoint);
        //}

        //private void ZoomOut()
        //{
        //    if (this.zoomOutTimer.IsEnabled || this.zoomInTimer.IsEnabled)
        //        return;

        //    this.zoomOutTimer.Start();
        //}

        //private void ZoomIn()
        //{
        //    if (this.zoomOutTimer.IsEnabled || this.zoomInTimer.IsEnabled)
        //        return;

        //    this.zoomInTimer.Start();
        //}

        //private void LocationBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        //{

        //   // SetProvider();
        //    //this.ZoomOut();
        //    // this.ZoomIn();
        //}
    }
}